package com.aso114.project.mvp.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.ChapterContactAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.bean.QuestionUserListBean;
import com.aso114.project.exam.QuestionTestActivity;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;
import com.aso114.project.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 章节练习
 * Created by asus on 2018/11/29.
 */

public class ChapterContactActivity extends BaseSimpleActivity implements ChapterContactAdapter.OnItemClick {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.chapter_liatView)
    RecyclerView chapterLiatView;
    Dialog dialog;
    ChapterContactAdapter chapterContactAdapter;
    Intent intent;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.null_ly)
    LinearLayout nullLy;
    private List<QuestionUserListBean> listBean = new ArrayList<>();
    QuestionBean qdata;

    @Override
    protected int getResouseId() {
        return R.layout.activity_chapter_contact;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("章节练习");
        chapterLiatView.setLayoutManager(new LinearLayoutManager(this));
        chapterContactAdapter = new ChapterContactAdapter();
        chapterLiatView.setAdapter(chapterContactAdapter);
        chapterContactAdapter.setDatas(listBean);

        final View dialogview = ChapterContactActivity.this.getLayoutInflater().inflate(
                R.layout.loading_view, null);
        dialog = new Dialog(ChapterContactActivity.this, R.style.dialogStyle);
        dialog.setContentView(dialogview, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setGravity(Gravity.CENTER);
        // 设置点击外围解散
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);//设置是否可以按退回键取消
        dialog.show();

        CommentModel.getInstant().getSpecialTreeList(Constant.adutst, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                dialog.cancel();
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                List<QuestionUserListBean> bean = (List<QuestionUserListBean>) o;
                if (bean == null || bean.size() == 0) {
                    nullLy.setVisibility(View.VISIBLE);
                    chapterLiatView.setVisibility(View.GONE);
                } else {
                    nullLy.setVisibility(View.GONE);
                    chapterLiatView.setVisibility(View.VISIBLE);

                    listBean = bean;
                    chapterContactAdapter.setDatas(bean);
                    chapterContactAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String fail) {
                dialog.cancel();
            }
        });
        chapterContactAdapter.setItemClick(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.layout_title_bar_back_iv)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void click(String id) {

        final View dialogview = ChapterContactActivity.this.getLayoutInflater().inflate(
                R.layout.loading_view, null);
        dialog = new Dialog(ChapterContactActivity.this, R.style.dialogStyle);
        dialog.setContentView(dialogview, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setGravity(Gravity.CENTER);
        // 设置点击外围解散
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);//设置是否可以按退回键取消
        dialog.show();
        // 点击章节练习  进入练习试卷
        CommentModel.getInstant().get_chapter_bank(LoginSP.getInstance().getUserAreaId(), id, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                dialog.cancel();
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                qdata = (QuestionBean) o;

                System.out.println("题库大小111===" + qdata.getPaperTitle());//getQuestionBaseList()
                // .get(0).getQuestionList().size()

                System.out.println("题库数组大小===" + qdata.getQuestionBaseList().get(0)
                        .getQuestionList().size());


                EventBus.getDefault().postSticky(qdata);

                intent = new Intent(ChapterContactActivity.this, QuestionTestActivity.class);
//                intent.putExtra("paperid", mdata .getPaperId());
                intent.putExtra("isAnalysis", true);
                intent.putExtra("is_exam", false);
                startActivity(intent);
            }

            @Override
            public void onFailure(String fail) {
                ToastUtils.showToast(ChapterContactActivity.this, "获取题库失败");
                dialog.cancel();
            }
        });
    }
}
